//给你一个单链表的头节点 head ，请你判断该链表是否为回文链表。如果是，返回 true ；否则，返回 false 。 
//
// 
//
// 示例 1： 
//
// 
//输入：head = [1,2,2,1]
//输出：true
// 
//
// 示例 2： 
//
// 
//输入：head = [1,2]
//输出：false
// 
//
// 
//
// 提示： 
//
// 
// 链表中节点数目在范围[1, 10⁵] 内 
// 0 <= Node.val <= 9 
// 
//
// 
//
// 进阶：你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题？ 
// Related Topics 栈 递归 链表 双指针 👍 1316 👎 0

package leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class _234_PalindromeLinkedList {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _234_PalindromeLinkedList().new Solution();
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(2, new ListNode(1))));
//        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(1))));
        System.out.println(solution.isPalindrome(head));
    }


    // 空间复杂度1
    // TODO 这个反转链表没太懂
    class Solution {
        public boolean isPalindrome(ListNode head) {
            ListNode fast = head;
            ListNode slow = head;
            int count = 0;
            while (fast != null) {
                count++;
                if (count % 2 == 0) slow = slow.next;
                fast = fast.next;
            }

            ListNode end = reverseList(slow);
            ListNode tmp = end;

            while (head != null && end != null) {
                if (head.val != end.val) {
                    return false;
                }
                head = head.next;
                end = end.next;
            }
            reverseList(tmp);

            return true;
        }

        private ListNode reverseList(ListNode head) {
            ListNode pre = null;
            while (head != null) {
                ListNode next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }
    }

    class Solution3 {
        public boolean isPalindrome(ListNode head) {
            int cnt = 0;
            ListNode cur = head;
            while (cur != null) {
                cnt++;
                cur = cur.next;
            }
            Deque<Integer> stack = new ArrayDeque<>();
            for (int i = 0; i < cnt / 2; i++) {
                stack.addLast(head.val);
                head = head.next;
            }
            if (cnt % 2 != 0) head = head.next;
            while (!stack.isEmpty()) {
                if (stack.removeLast() != head.val) {
                    return false;
                }
                head = head.next;
            }
            return true;
        }
    }

    class Solution2 {
        public boolean isPalindrome(ListNode head) {
            ListNode cur = head;
            List<Integer> list = new ArrayList<>();
            while (cur != null) {
                list.add(cur.val);
                cur = cur.next;
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) != list.get(list.size() - 1 - i)) {
                    return false;
                }
            }
            return true;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    // 暴力解法，换成数组
    class Solution1 {
        public boolean isPalindrome(ListNode head) {
            ListNode cur = head;
            List<Integer> list = new ArrayList<>();
            while (cur != null) {
                list.add(cur.val);
                cur = cur.next;
            }
            Integer[] nums = list.toArray(new Integer[list.size()]);
            for (int i = 0; i < nums.length / 2; i++) {
                if (nums[i] != nums[nums.length - i - 1]) {
                    return false;
                }
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}